package sm.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.ApplicationContext;

import sm.hibernate.DAMUtil;
import sm.upgrade.api.DataService;
import sm.upgrade.util.KeyValueChangePolicy;
import sm.upgrade.util.UpgradeContext;
import sm.util.SMMap;
import sm.util.Util;

public class RestoreKey {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String fileName = "smenv";
		String[] keyPaths = new String[] { "name" };

		UpgradeContext context = UpgradeContext.getInstance();
		ApplicationContext customerContext = context
				.getOrCreateCustomerContext();
		DataService customerDataService = (DataService) customerContext
				.getBean("dataService");
		KeyValueChangePolicy policy = (KeyValueChangePolicy) customerContext
				.getBean(fileName + "KeyValueChangePolicy");

		String entityName = Util.radEntityNameToJava(fileName);

		List<SMMap> list = customerDataService.getNextBatchDatas(entityName, 0,
				0);

		List<SMMap> updateList = new ArrayList<SMMap>();
		for (SMMap clonedData : list) {
			Object[] keyValues = DAMUtil.getValues(clonedData, keyPaths);
			List<Object> oldKeyValues = (List<Object>) policy.getOldKey(Arrays
					.asList(keyValues));
			if (oldKeyValues != null) {
				System.out.println("find old key: " + oldKeyValues.toString());
				DAMUtil.setValues(clonedData, keyPaths, oldKeyValues.toArray());
				updateList.add(clonedData);
			}
		}

		// customerDataService.updateDatas(fileName, updateList);

	}
}
